package com.lab.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.lab.dao.MajorDao;
import com.lab.po.Major;
import com.lab.po.Student;
import com.lab.util.MySqlSessionFactory;

public class MajorDaoImpl implements MajorDao{
	//根据学院id查询所有的专业
	@Override
	public List<Major> queryMajor(Map map) {
		SqlSession session=null;
		List<Major> majorList=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			majorList=session.selectList("MajorXml.queryMajor",map);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return majorList;
	}
	
	//根据id查找专业名
	@Override
	public String queryMajorName(int maj_id) {
		SqlSession session=null;
		String maj_name=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			maj_name=session.selectOne("MajorXml.queryMajorName",maj_id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return maj_name;
	}

	@Override
	public int queryMajorByName(String maj_name) {
		SqlSession session=null;
		int i=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.selectOne("MajorXml.queryMajorByName",maj_name);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}

	@Override
	public int queryMaxMaj_id() {
		SqlSession session=null;
		int i=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.selectOne("MajorXml.queryMaxMaj_id");
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i+1;
	}

	@Override
	public int addMajor(Map map) {
		SqlSession session=null;
		int i=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.insert("MajorXml.addMajor",map);
			System.out.println(i);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}		
		return i;
	}
	
	//查询某个学院的专业数量
	@Override
	public int countMajorByCol_id(int col_id) {
		SqlSession session=null;
		int i=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.selectOne("MajorXml.countMajorByCol_id",col_id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}		
		return i;
	}
	
	//根据id查询专业信息
	@Override
	public Major queryMajorByMaj_id(int maj_id) {
		SqlSession session=null;
		Major major=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			major=session.selectOne("MajorXml.queryMajorByMaj_id", maj_id);
			session.commit();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return major;
	}
	
	//更新专业
	@Override
	public int updateMajor(Map map) {
		int i=0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();		
			i = session.update("MajorXml.updateMajor",map);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}
	
	//删除专业
	@Override
	public int deleteMajor(int maj_id) {
		int i = 0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.delete("MajorXml.deleteMajor", maj_id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}

}
